﻿using Md.Api.Entity.DapperModels.MdDB;
using Md.Api.Model.Manage.SysConfig.Request;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 系统参数配置仓储
/// </summary>
public class SysConfigRep : BaseRep<SysConfigModel>
{
    private static string sqlFields = string.Format(@"
         `config_id`
        ,`config_name`
        ,`config_key`
        ,`config_value`
        ,`config_type`
        ,`create_by`
        ,`create_time`
        ,`update_by`
        ,`update_time`
        ,`remark`
    ");

    /// <summary>
    /// 通过配置编号获取系统配置信息
    /// </summary>
    /// <param name="configId">配置编号</param>
    /// <returns>SysConfigModel?</returns>
    public SysConfigModel? GetSysConfigByConfigId(int configId)
    {
        string sqlWhere = " and `config_id` = @ConfigId ";

        return QuerySingle(sqlFields, sqlWhere, new { ConfigId = configId });
    }

    /// <summary>
    /// 通过配置KEY获取系统配置信息
    /// </summary>
    /// <param name="configKey">配置KEY</param>
    /// <returns>SysConfigModel?</returns>
    public SysConfigModel? GetSysConfigByConfigKey(string configKey)
    {
        string sqlWhere = " and `config_key` = @ConfigKey ";

        return QuerySingle(sqlFields, sqlWhere, new { ConfigKey = configKey });
    }

    /// <summary>
    /// 获取系统配置列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>IEnumerable<SysConfigModel></returns>
    public IEnumerable<SysConfigModel> GetSysConfigList(SysConfigModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.ConfigName))
        {
            sqlWhere += " and `config_name` like concat('%', @ConfigName ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigKey))
        {
            sqlWhere += " and `config_key` like concat('%', @ConfigKey ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigValue))
        {
            sqlWhere += " and `config_value` like concat('%', @ConfigValue ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigType))
        {
            sqlWhere += " and `config_type` = @ConfigType ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取系统配置列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<SysConfigModel></returns>
    public Page<SysConfigModel> GetSysConfigPageList(SysConfigModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.ConfigName))
        {
            sqlWhere += " and `config_name` like concat('%', @ConfigName ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigKey))
        {
            sqlWhere += " and `config_key` like concat('%', @ConfigKey ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigValue))
        {
            sqlWhere += " and `config_value` like concat('%', @ConfigValue ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigType))
        {
            sqlWhere += " and `config_type` = @ConfigType ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过配置编号更新系统配置
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateSysConfigByConfigId(SysConfigModel model)
    {
        string sqlSet = string.Empty;

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.ConfigName))
        {
            sqlSet += " `config_name` = @ConfigName, ";
        }

        if (!string.IsNullOrWhiteSpace(model.ConfigKey))
        {
            sqlSet += " `config_key` = @ConfigKey, ";
        }

        if (!string.IsNullOrWhiteSpace(model.ConfigValue))
        {
            sqlSet += " `config_value` = @ConfigValue, ";
        }

        if (!string.IsNullOrWhiteSpace(model.ConfigType))
        {
            sqlSet += " `config_type` = @ConfigType, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        if (model.Remark != null)
        {
            sqlSet += " `remark` = @Remark, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `config_id` = @ConfigId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过系统配置编号删除系统配置
    /// </summary>
    /// <param name="configId">系统配置编号</param>
    /// <returns>int</returns>
    public int DeleteSysDictDataByDictCode(long configId)
    {
        string sqlWhere = " and `config_id` = @ConfigId ";

        return Delete(sqlWhere, new { ConfigId = configId });
    }
}

